/*
 * Copyright (C) 2009 Rob Cash
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * SubscriptionTableModel.java
 * 
 * Created on Jan 19, 2009 at 1:15:40 PM
 */

package org.robcash.apps.messagepopup.view.components;


import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import org.robcash.apps.messagepopup.vo.Schedule;
import org.robcash.apps.messagepopup.vo.Subscription;

/**
 *
 * @author Rob
 */
public class SubscriptionTableModel extends AbstractTableModel
        implements TableModel {

    private static final int SUBSCRIPTION_COLUMN = 0;
    private static final int SCHEDULE_COLUMN = 1;
    
    private List<Subscription> subscriptions;

    public SubscriptionTableModel() {
        subscriptions = new ArrayList<Subscription>();
    }

    public int getColumnCount() {
        return 2;
    }

    @Override
    public String getColumnName(int columnIndex) {
        if ( columnIndex == SUBSCRIPTION_COLUMN ) {
            return "Subscription";
        }
        else if ( columnIndex == SCHEDULE_COLUMN ) {
            return "Schedule";
        }
        else {
            return super.getColumnName(columnIndex);
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        if ( columnIndex == SUBSCRIPTION_COLUMN ) {
            return Subscription.class;
        }
        else if ( columnIndex == SCHEDULE_COLUMN ) {
            return Schedule.class;
        }
        else {
            // TODO Figure out what to do
            return Object.class;
        }
    }

    public int getRowCount() {
        return subscriptions.size();
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Subscription aSubscription = subscriptions.get(rowIndex);
        if ( columnIndex == SUBSCRIPTION_COLUMN ) {
            return aSubscription;
        }
        else if ( columnIndex == SCHEDULE_COLUMN ) {
            return aSubscription.getSchedule();
        }
        else {
            // TODO Figure out what to do
            return null;
        }
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        // Schedule should be editable thru a drop-down
        return columnIndex == SCHEDULE_COLUMN;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        Subscription aSubscription = subscriptions.get(rowIndex);
        if ( columnIndex == SUBSCRIPTION_COLUMN ) {
            aSubscription.setName((String) aValue);
            fireTableCellUpdated(rowIndex, columnIndex);
        }
        else if ( columnIndex == SCHEDULE_COLUMN ) {
            aSubscription.setSchedule((Schedule) aValue);
            fireTableCellUpdated(rowIndex, columnIndex);
        }
        else {
            // TODO Figure out what to do
        }
    }

    public void add(Subscription subscription) {
        subscriptions.add(subscription);
        int index = subscriptions.size() - 1;
        fireTableRowsInserted(index, index);
    }

    public void addAll(Collection<Subscription> newSubscriptions) {
        int startIndex = subscriptions.size();
        subscriptions.addAll(newSubscriptions);
        int endIndex = subscriptions.size() - 1;
        fireTableRowsInserted(startIndex, endIndex);
    }

}
